home *** CD-ROM | disk | FTP | other *** search
/ SunSoft Catalyst CDWARE 1996 May to August / Catalyst CDWARE 1996 May to August.iso / .products / SunSoft.CafeDelSol / JAVA / Comix1.java < prev    next >
Text File  |  1996-01-19  |  7KB  |  249 lines

  1. /* Comix, Copyright 1996, Eric C Harshbarger.
  2. This code is available for public use and modification as long as this notice of copyright is kept in tact. Additional credit is allowable for modifications.
  3.  
  4. Documentation on this applet may be found at:
  5. http://www.auburn.edu/^harshec/WWW/slideshow.html
  6.  
  7. Questions or comments should be directed to:
  8.  
  9. Eric Harshbarger ( http://www.auburn.edu/^harshec/ )
  10. harshec@mail.auburn.edu   OR    harshec@cdware.eng.sun.com   */
  11.  
  12. import java.applet.*;
  13. import java.awt.*;
  14. import java.util.StringTokenizer;
  15. import java.util.Vector;
  16. import java.lang.*;
  17. import java.util.Hashtable;
  18. import java.net.URL;
  19. import java.awt.image.*;
  20. import java.net.MalformedURLException;
  21.  
  22. public class Comix1 extends Applet implements Runnable {
  23.  
  24.     Thread woohoo;
  25.     Image background;
  26.     Image bufimage;
  27.     Graphics bufgraphic;
  28.     Dimension roo;
  29.     String param,pp,fonttype;
  30.     String parampiece;
  31.     String type[] = new String[6];
  32.     String words[] = new String[6];
  33.     String mouth[] = new String[6];
  34.     int a=0;
  35.     int fontsize;
  36.     int bubblex[] = new int[6];
  37.     int bubbley[] = new int[6];
  38.     boolean bubbleon[] = new boolean[6];
  39.  
  40.     public void init() {
  41.         roo = this.size();
  42.         bufimage = createImage(roo.width,roo.height);
  43.         bufgraphic = bufimage.getGraphics();
  44.  
  45.         pp = getParameter("FONTSIZE");
  46.         fontsize = (pp != null) ? Integer.parseInt(pp) : 14;
  47.  
  48.         pp = getParameter("FONTTYPE");
  49.         fonttype = (pp != null) ? pp : "Courier";
  50.     }
  51.  
  52.     public void paint(Graphics g) {
  53.         paintBuffer(bufgraphic);
  54.         g.drawImage(bufimage,0,0,this);
  55.     }
  56.  
  57.     public void paintBuffer(Graphics g) {
  58.         g.setColor(Color.lightGray);
  59.         g.fillRect(0,0,roo.width,roo.height);
  60.         if (background != null) { g.drawImage(background,0,0,this); }
  61.         for (int i=0;i<6;i++) {
  62.             if (bubbleon[i]) {
  63.                 drawSentence(g,words[i],mouth[i],bubblex[i],bubbley[i]);
  64.             }
  65.         }
  66.  
  67.         g.setColor(Color.black);
  68.         g.drawRect(0,0,roo.width-1,roo.height-1);
  69.         g.setColor(Color.white);
  70.         for (int i=1;i<4;i++) {
  71.             g.drawRect(i,i,roo.width-2*i-1,roo.height-2*i-1);
  72.         }
  73.         g.setColor(Color.black);
  74.         g.drawRect(4,4,roo.width-9,roo.height-9);
  75.     }
  76.  
  77.     public void drawSentence(Graphics g, String sentence, String mouth, int x, int y) {
  78.         int ovalx,ovaly;
  79.         Font f = new Font(fonttype,0,fontsize);
  80.         g.setFont(f);
  81.         FontMetrics fm = g.getFontMetrics();
  82.         StringTokenizer st1 = new StringTokenizer(sentence+"/","/");
  83.         int lines = st1.countTokens();
  84.         int linex[] = new int[lines];
  85.         String liness[] = new String[lines];
  86.         int longestline = 0;
  87.         int i=0;
  88.         while (st1.hasMoreTokens()) {
  89.             liness[i] = st1.nextToken();
  90.             StringTokenizer st3 = new StringTokenizer(liness[i],"{");
  91.             linex[i] = fm.stringWidth(liness[i])-(st3.countTokens()-1)*(fm.stringWidth("{")*3);
  92.             longestline = Math.max(longestline,linex[i]);
  93.             i++;
  94.         }
  95.         int bubwidth = (int)(longestline*1.25);
  96.         int bubheight = (int)((fm.getHeight()*lines+fm.getLeading()*(lines-1))*2);
  97.  
  98.         if (mouth.equals("ne")) { ovalx = x; ovaly = y-bubheight; }
  99.         else if (mouth.equals("se")) { ovalx = x; ovaly = y; }
  100.         else if (mouth.equals("sw")) { ovalx = x-bubwidth; ovaly = y; }
  101.         else { ovalx = x-bubwidth; ovaly = y-bubheight; }
  102.  
  103.         g.setColor(Color.white);
  104.         g.fillOval(ovalx,ovaly,bubwidth,bubheight);
  105.         g.setColor(Color.black);
  106.         g.drawOval(ovalx,ovaly,bubwidth,bubheight);
  107.         g.setColor(Color.white);
  108.  
  109.         int polyx[] = new int[3];
  110.         int polyy[] = new int[3];
  111.         polyx[0] = x;
  112.         polyy[0] = y;
  113.         polyx[1] = ovalx+bubwidth/2-bubwidth/6;
  114.         polyy[1] = ovaly+bubheight/2;
  115.         polyx[2] = ovalx+bubwidth/2+bubwidth/6;
  116.         polyy[2] = ovaly+bubheight/2;
  117.         g.fillPolygon(polyx,polyy,3);
  118.  
  119.         g.setColor(Color.black);
  120.  
  121.         g.drawLine(polyx[0],polyy[0],polyx[1],polyy[1]);
  122.         g.drawLine(polyx[0],polyy[0],polyx[2],polyy[2]);
  123.  
  124.         g.setColor(Color.white);
  125.         g.fillOval(ovalx+1,ovaly+1,bubwidth-2,bubheight-2);
  126.  
  127.         g.setColor(Color.black);
  128.  
  129.         String currentword;
  130.         int centerx = ovalx+bubwidth/2;
  131.         int centery = ovaly+bubheight/2;
  132.         int startx,starty;
  133.  
  134.         for (int j=0;j<lines;j++) {
  135.             StringTokenizer st = new StringTokenizer(liness[j]);
  136.             startx = centerx - linex[j]/2;
  137.             int textheight = (lines*fm.getHeight());
  138.             starty = (int)(centery-textheight/2+(j+1)*fm.getHeight()-lines);
  139.             while (st.hasMoreTokens()) {
  140.                 currentword = st.nextToken();
  141.                 boolean tag = false;
  142.                 tag = (currentword.indexOf("{") != -1) ? true : false;
  143.                 if (tag) {
  144.                     if (currentword.substring(1,2).equalsIgnoreCase("b")) {
  145.                         currentword = currentword.substring(3);
  146.                         f = new Font(fonttype,Font.BOLD,fontsize);
  147.                         g.setFont(f);
  148.                         g.drawString(currentword,startx,starty);
  149.                     }
  150.                     else if (currentword.substring(1,2).equalsIgnoreCase("i")) {
  151.                         currentword = currentword.substring(3);
  152.                         f = new Font(fonttype,Font.ITALIC,fontsize);
  153.                         g.setFont(f);
  154.                         g.drawString(currentword,startx,starty);
  155.                     }
  156.                     else if (currentword.substring(1,2).equalsIgnoreCase("u")) {
  157.                         currentword = currentword.substring(3);
  158.                         f = new Font(fonttype,Font.PLAIN,fontsize);
  159.                         g.setFont(f);
  160.                         g.drawString(currentword,startx,starty);
  161.                         g.drawLine(startx,starty+1,startx+fm.stringWidth(currentword),starty+1);
  162.                     }
  163.                 }
  164.                 else {
  165.                     f = new Font(fonttype,Font.PLAIN,fontsize);
  166.                     g.setFont(f);
  167.                     g.drawString(currentword,startx,starty);
  168.                 }
  169.                 fm = g.getFontMetrics();
  170.                 startx = startx+fm.stringWidth(currentword+" ");
  171.             }
  172.         }
  173.     }
  174.  
  175.     public void update(Graphics g) {
  176.         paint(g);
  177.     }
  178.  
  179.     public void start() {
  180.         if (woohoo == null) {
  181.             woohoo = new Thread(this);
  182.             woohoo.start();
  183.         }
  184.     }
  185.  
  186.     public void stop () {
  187.         woohoo = null;
  188.     }
  189.  
  190.     public void run() {
  191.         int pipe;
  192.  
  193.         while (getParameter("ACTION"+a) != null) {
  194.  
  195.         param = getParameter("ACTION"+a);
  196.         if (param != null) {
  197.             pipe = param.indexOf("|");
  198.             parampiece = param.substring(0,pipe);
  199.             if (parampiece.indexOf("BUBBLE") != -1) {
  200.                 int bubblenumber = Integer.parseInt(parampiece.substring(6));
  201.                 bubbleon[bubblenumber] = true;
  202.                 StringTokenizer st = new StringTokenizer(param.substring(pipe+1),"|");
  203.                 while (st.hasMoreTokens()) {
  204.                     type[bubblenumber] = st.nextToken();
  205.                     mouth[bubblenumber] = st.nextToken();
  206.                     bubblex[bubblenumber] = Integer.parseInt(st.nextToken());
  207.                     bubbley[bubblenumber] = Integer.parseInt(st.nextToken());
  208.                     words[bubblenumber] = st.nextToken();
  209.                     repaint();
  210.                 }
  211.             }
  212.             else if (parampiece.equals("WAIT")) {
  213.                 int pause = Integer.parseInt(param.substring(pipe+1));
  214.                 try {Thread.sleep(pause);} catch (InterruptedException e){};
  215.             }
  216.             else if (parampiece.equals("CLEAR")) {
  217.                 StringTokenizer st = new StringTokenizer(param.substring(pipe+1),"|");
  218.                 while (st.hasMoreTokens()) {
  219.                     int bubblenumber = Integer.parseInt(st.nextToken().substring(6));
  220.                     bubbleon[bubblenumber] = false;
  221.                     repaint();
  222.                 }
  223.             }
  224.             else if (parampiece.equals("BACKGROUND")) {
  225.                 background = getImage(getDocumentBase(),param.substring(pipe+1));
  226.                 repaint();
  227.             }
  228.             else if (parampiece.equals("LOOPBACKTO")) {
  229.                 a = Integer.parseInt(param.substring(pipe+1))-1;
  230.             }
  231.         }
  232.         a++;
  233.         }
  234.     }
  235.  
  236.  
  237. /*    public boolean mouseEnter(java.awt.Event evt, int mx, int my) {
  238.     }
  239.  
  240.     public boolean mouseExit(java.awt.Event evt, int mx, int my) {
  241.     }
  242.  
  243.     public boolean mouseDown(java.awt.Event evt, int mx, int my) {
  244.     }*/
  245.  
  246.  
  247.  
  248. }
  249.